﻿#include "surface.h"
#include <QApplication>

Surface::Surface(QWidget *parent) : QWidget(parent)
{

}


Surface::~Surface()
{
    if (reader != nullptr)
    {
        reader->stop();
        disconnect(reader, &Reader::updateImage, this, &Surface::onUpdateImage);
        disconnect(reader, &Reader::errorOccurred, this, &Surface::onError);
        disconnect(reader, &Reader::openCompleted, this, &Surface::onOpen);
        delete reader;
    }
}

/**
 * 一个鼠标的点击事件监听
 * @brief Surface::mousePressEvent
 * @param event
 */
bool Surface::init(QString filename)
{
    if (reader != nullptr)
    {
        return false;
    }
    m_filename = filename;

    av_register_all();
    avcodec_register_all();
    avformat_network_init();

    //m_filename = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
    //m_filename = "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";

    reader = new Reader(m_filename);
    // 链接一个信号，用于定时更新界面
    connect(reader, &Reader::updateImage, this, &Surface::onUpdateImage);
    connect(reader, &Reader::errorOccurred, this, &Surface::onError, Qt::QueuedConnection);
    connect(reader, &Reader::openCompleted, this, &Surface::onOpen, Qt::QueuedConnection);

    reader->open();
    return true;

//    // 监听的内容是鼠标右键点击的事件
//    if(event->button() == Qt::RightButton)
//    {
//        qDebug() << "!!!!";
//        QString filename = QFileDialog::getOpenFileName();
//        if(!filename.isEmpty())
//        {
//            qDebug() << "surface filename : " << filename;
//            // 创建一个Reader，用于处理数据
//            reader = new Reader(filename) ;
//            // 链接一个信号，用于定时更新界面
//            connect(reader , SIGNAL(onUpdateImage(QImage)) , this , SLOT(getNewImage(QImage)));
//        }
//    }
}

void Surface::onUpdateImage(QImage image)
{
    this->image = image;
    update();
}


void Surface::onError()
{
    if (isOpen)
    {
        refresh();
    }
}


void Surface::onOpen()
{
    isOpen = true;
}

void Surface::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    if (image.isNull())
    {
        painter.setBrush(Qt::white);
        painter.drawRect(0, 0, this->width(), this->height()); //先画成白色
        return;
    }

    QImage img = image.scaled(size(),
        Qt::KeepAspectRatio, Qt::SmoothTransformation);

    int x = this->width() - img.width();
    int y = this->height() - img.height();

    x /= 2;
    y /= 2;

    painter.drawImage(QPoint(x, y), img); //画出图像
}

void Surface::refresh()
{
    if (reader != nullptr)
    {
        reader->stop();
        disconnect(reader, &Reader::updateImage, this, &Surface::onUpdateImage);
        disconnect(reader, &Reader::errorOccurred, this, &Surface::onError);
        disconnect(reader, &Reader::openCompleted, this, &Surface::onOpen);
        delete reader;
        reader = nullptr;
    }

    init(m_filename);
}

//int main(int argc , char * argv[])
//{
//    QApplication app(argc , argv);
//
//    av_register_all();
//    avcodec_register_all();
//    avformat_network_init();
//
//    Surface surface;
//    surface.show();
//
//    return app.exec();
//}
